conf = require("../config")
if not conf.telescope.enabled then return {} end

local function choose_templates()
  local templates_dir = vim.fn.expand("~/.config/nvim/.vscode")
  local template_files = vim.fn.glob(templates_dir .. "/*", false, true)

  local template_names = {}
  for _, file in ipairs(template_files) do
    table.insert(template_names, vim.fn.fnamemodify(file, ":t"))
  end

  vim.ui.select(template_names, { prompt = "Choose a template:" },
  function (choice)
    if not choice then return end


    local current_dir = vim.fn.getcwd()
    local target_dir = current_dir .. "/.vscode"
    if vim.fn.isdirectory(target_dir) == 0 then
        vim.fn.mkdir(target_dir, "p")
    end

    local template_file = templates_dir .. "/" .. choice
    vim.fn.system(string.format("cp '%s' '%s'", template_file, target_dir))
    vim.notify("Template " .. choice .. ".json has been created!", vim.log.levels.INFO)
  end)
end


return {
  "nvim-telescope/telescope.nvim",
  dependencies = {
    'nvim-lua/plenary.nvim',
    'nvim-telescope/telescope-ui-select.nvim',
  },
  keys = {
    {",ff", function () require('telescope.builtin').find_files() end, "n", desc = "find files"},
    {",fg", function () require('telescope.builtin').live_grep() end, "n", desc = "search for a string and get results live"},
    {",fb", function () require('telescope.builtin').buffers() end, "n", desc = "find buffers"},
    {",fh", function () require('telescope.builtin').help_tags() end, "n", desc = "list available help tags"},
    {",fc", function () require('telescope.builtin').commands() end, "n", desc = "lists available plugin/user commands"},
    {",fj", function () choose_templates() end, "n", desc = "choose templates"},
    {"gr", function () require('telescope.builtin').lsp_references() end, "n", desc = "list all the references to the symbol"},
    {"gi", function () require('telescope.builtin').lsp_implementations() end, "n", desc = "list all the implementations"},
    {"gd", function () require('telescope.builtin').lsp_definitions() end, "n", desc = "jumps to the definition"},
    {",fp", "<cmd>Telescope projects<cr>", "n", desc = "find projects"},
  },
  cmd = "Telescope",
  config =function ()
    require("telescope").load_extension("ui-select")
    require('telescope').load_extension('projects')

    require("telescope").setup({
      defaults = {
        -- Default configuration for telescope goes here:
        -- config_key = value,
        mappings = {
          i = {
            -- map actions.which_key to <C-h> (default: <C-/>)
            -- actions.which_key shows the mappings for your picker,
            -- e.g. git_{create, delete, ...}_branch for the git_branches picker
            -- ["<C-h>"] = "which_key"
          }
        }
      },
      pickers = {
        -- Default configuration for builtin pickers goes here:
        -- picker_name = {
        --   picker_config_key = value,
        --   ...
        -- }
        -- Now the picker_config_key will be applied every time you call this
        -- builtin picker
      },
      extensions = {
        -- Your extension configuration goes here:
        -- extension_name = {
        --   extension_config_key = value,
        -- }
        -- please take a look at the readme of the extension you want to configure
        ["ui-select"] = {
          require("telescope.themes").get_dropdown {
            -- even more opts
          }

          -- pseudo code / specification for writing custom displays, like the one
          -- for "codeactions"
          -- specific_opts = {
          --   [kind] = {
          --     make_indexed = function(items) -> indexed_items, width,
          --     make_displayer = function(widths) -> displayer
          --     make_display = function(displayer) -> function(e)
          --     make_ordinal = function(e) -> string
          --   },
          --   -- for example to disable the custom builtin "codeactions" display
          --      do the following
          --   codeactions = false,
          -- }
        }
      },
    })
  end,
}
